
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.Vector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Laurens
 */
public class CorrectFile 
{
    public CorrectFile()
    {
        
        
    }
    
    public static void main(String args[]) throws FileNotFoundException
    {
    }
    
    public String returnCorrectFile(String file)
    {
        //String spaceFree = removeSpaces(file);
        Scanner scanner = new Scanner(file);
        Vector<String> result = new Vector<String>();
        boolean cleanLine = false;
        while(scanner.hasNext())
        {
            String currentLine = scanner.nextLine();
            if(currentLine.contains("public "))
            {
                if(currentLine.contains(" class "))
                {
                    String currentLine2 = scanner.nextLine();
                    if(currentLine.contains("{"))
                    {
                        result.add(currentLine);
                    }
                    else if(currentLine2.contains("{"))
                    {
                        result.add(currentLine + "\n" +currentLine2);
                    }
                    else
                    {
                        String current = scanner.nextLine();
                        while(!current.contains("{"))
                        {
                            current = scanner.nextLine();
                        }
                        result.add(currentLine + "\n" + current);
                    }
                }
                else if(currentLine.contains(" interface "))
                {
                    String currentLine2 = scanner.nextLine();
                    if(currentLine.contains("{"))
                    {
                        result.add(currentLine);
                    }
                    else if(currentLine2.contains("{"))
                    {
                        result.add(currentLine + " " +currentLine2);
                    }
                    else
                    {
                        String current = scanner.nextLine();
                        while(!current.contains("{"))
                        {
                            current = scanner.nextLine();
                        }
                        result.add(currentLine + "\n" + current);
                    }
                }
                else 
                {
                    if(currentLine.contains("("))
                    {
                        result.add(currentLine);
                    }
                    else
                    {
                        String current = scanner.nextLine();
                        String out = scanner.nextLine();
                        while(!current.contains(")"))
                        {
                            current = scanner.nextLine();
                            if(current.contains("//"))
                            {

                            }
                            else
                            {
                                out += current;
                            }
                        }
                        result.add(currentLine+removeCommentsInsideBrackets(out.trim()));
                    }
                }
            }   
            else
            {
                if(currentLine.contains("/*"))
                {
                    if(currentLine.contains("*/"))
                    {
                        StringTokenizer st = new StringTokenizer(currentLine);
                        while(st.hasMoreTokens())
                        {
                            String current = st.nextToken();
                            if(current.contains(";"))
                            {
                                cleanLine = true;
                            }
                            else if(current.contains(")"))
                            {
                                cleanLine = true;
                            }
                            else if(currentLine.contains("else"))
                            {
                                cleanLine = true;
                            }
                            else if(currentLine.contains("if"))
                            {
                                cleanLine = true;
                            }
                        }
                    }
                }
                else
                {
                    cleanLine = false;
                }
                if(cleanLine)
                {
                    result.add(cleanLine(currentLine));
                }
                else
                {
                    result.add(currentLine);
                }
            }
        }
        String output = "";
        for(int i = 0; i < result.size(); i++)
        {
            output += result.get(i) + "\n";
        }
        String anotherOutput = removeMethodSpaces(output);
        //String removeCurlyOutput = "";
        //removeCurlyOutput = removeFunctionCurly(output);
        return anotherOutput;
    }
    
    public String removeCommentsInsideBrackets(String current)
    {
        StringTokenizer st = new StringTokenizer(current," ");
        String output = "";
        while(st.hasMoreTokens())
        {
            String currentToken = st.nextToken();
            if(currentToken.contains("("))
            {
                if(currentToken.contains(")"))
                {
                    output += currentToken + " ";
                }
                else
                {
                    output += currentToken + " ";
                    String anotherCurrent = st.nextToken();
                    while(!anotherCurrent.contains(")"))
                    {
                        output += anotherCurrent + " ";
                        anotherCurrent = st.nextToken();
                    }
                    output += anotherCurrent;
                }
            }
        }
        output = removeComments(output);
        return output;
    }

    private String removeComments(String output) 
    {
        String result = "";
        StringTokenizer st = new StringTokenizer(output);
        while(st.hasMoreTokens())
        {
            String current = st.nextToken();
            if(current.contains("/*"))
            {
                String comments = current;
                int i = current.indexOf("/*");
                int j = current.indexOf("*/");
                String needed = comments.substring(0, i);
                String neededAfter = comments.substring(j+2, comments.length());
                result += needed + " " + neededAfter + " ";
            }
            else
            {
                result += current + " ";
            }
        }
        return result;
    }

    private String removeFunctionCurly(String output) 
    {        
        Scanner scanner = new Scanner(output);
        Vector<String> result = new Vector<String>();
        while(scanner.hasNext())
        {   
            String currentLine = scanner.nextLine();
            if(currentLine.contains("public "))
            {
                if(currentLine.contains(" class "))
                {
                    result.add(currentLine);
                }
                else
                {
                    String current2 = scanner.nextLine();
                    if(currentLine.contains("{"))
                    {
                        result.add(currentLine);
                    }
                    else if(current2.contains("{"))
                    {
                        result.add(currentLine + "\n" + current2);
                    }
                    else
                    {
                        String current = scanner.nextLine();
                        while(!current.contains("{"))
                        {
                            current = scanner.nextLine();
                        }
                        result.add(currentLine + "\n" + current);
                    }
                }
            }
            else
            {
                result.add(currentLine);
            }
        }
        String output2 = "";
        for(int y = 0; y< result.size(); y++)
        {
            output2 += result.get(y) + "\n";
        }
        return output2;
    }
    
    public String cleanLine(String currentLine)
    {
        String result = "";
        StringTokenizer st = new StringTokenizer(currentLine);
        boolean endFound = false;
        while(st.hasMoreTokens())
        {
            String current = st.nextToken();
            if(current.contains(";"))
            {
                endFound = true;
            }
            if(!endFound)
            {
                if(current.contains("/*"))
                {
                    StringTokenizer st2 = new StringTokenizer(current, "/");
                    while(st2.hasMoreTokens())
                    {
                        String current2 = st2.nextToken();
                        if(current2.contains("*"))
                        {

                        }
                        else
                        {
                            result += current2 + " ";
                        }
                    }
                }
                else
                {
                    result += current + " ";
                }
            }
            else
            {
                result += current + " ";
            }
        }
        return result;
    }

//    private String removeSpaces(String file) 
//    {
//        Scanner scanner = new Scanner(file);
//        Vector<String> result = new Vector<String>();
//        while(scanner.hasNext())
//        {
//            String currentLine = scanner.nextLine();
//            if(currentLine.contains("{")){result.add(currentLine);}
//            else if(currentLine.contains("}")){result.add(currentLine);}
//            else if(currentLine.contains("(")){result.add(currentLine);}
//            else if(currentLine.contains(")")){result.add(currentLine);}
//            else if(currentLine.contains(";")){result.add(currentLine);}
//            else if(currentLine.contains("public")){result.add(currentLine);}
//            else if(currentLine.contains("*")){result.add(currentLine);}
//            else
//            {
//                StringTokenizer st = new StringTokenizer(currentLine, " ");
//                if(st.nextToken() == " ")
//                {
//                    
//                }
//            }
//        }
//        String output = "";
//        for(int i = 0; i < result.size(); i++)
//        {
//            output += result.get(i) + "\n";
//        }
//        return output;
//    }

    private String removeMethodSpaces(String file) 
    {
        Scanner scanner = new Scanner(file);
        Vector<String> result = new Vector<String>();
        while(scanner.hasNext())
        {
            String currentLine = scanner.nextLine();
            if(currentLine.contains(" public "))
            {
                if(!currentLine.contains(" abstract "))
                {
                    if(!currentLine.contains(" class "))
                    {
                        String currentLine2 = scanner.nextLine();
                        if(currentLine.contains("{"))
                        {
                            result.add(currentLine);
                        }
                        else if(currentLine2.contains("{"))
                        {
                            result.add(currentLine + "\n" + currentLine2);
                        }
                        else
                        {
                            String current = scanner.nextLine();
                            while(!current.contains("{"))
                            {
                                current = scanner.nextLine();
                            }
                            result.add(currentLine + "\n" + current);
                        }
                    }
                    else
                    {
                        result.add(currentLine);
                    }
                }
                else
                {
                    result.add(currentLine);
                }
            }
            else
            {
                result.add(currentLine);
            }
        }
        String output = "";
        for(int i = 0; i < result.size(); i++)
        {
            output += result.get(i) + "\n";
        }
        return output;
    }
}





